#include "chat.h"
#include "ui_chat.h"
#include<QPoint>
#include<QMouseEvent>
#include<QMessageBox>
#include<QFileDialog>
#include<QColorDialog>
#include<QFile>
#include<QTextStream>
#include<QIODevice>
chat::chat(QWidget *parent,QString memname):
    QWidget(parent),
    ui(new Ui::chat)
{
    ui->setupUi(this);
    setMouseTracking(true);
    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);

    this->ui->boxtitle->setText(memname);


    connect(ui->bold,&QPushButton::clicked,this,[=](bool checked){
        if(checked){
            ui->textEdit->setFontWeight(QFont::Bold);
        }
        else{
            ui->textEdit->setFontWeight(QFont::Normal);
        }
    });

    connect(ui->tip,&QPushButton::clicked,this,[=](bool checked){
        ui->textEdit->setFontItalic(checked);
        ui->textEdit->setFocus();
    });

    connect(ui->underline,&QPushButton::clicked,this,[=](bool checked){
        ui->textEdit->setFontUnderline(checked);
        ui->textEdit->setFocus();
    });

    connect(ui->clear,&QPushButton::clicked,this,[=](){
        ui->textBrowser->clear();
    });

    connect(ui->store,&QPushButton::clicked,this,[=](){
        if(ui->textBrowser->document()->isEmpty()){
            QMessageBox::warning(this,"警告","警告！保存记录不能为空!");
        }
        else{
            QString filename=QFileDialog::getSaveFileName(this,"保存聊天记录","聊天记录","(*.txt)");
            if(!filename.isEmpty()){
                QFile file(filename);
                file.open(QIODevice::WriteOnly|QFile::Text);
                QTextStream stream(&file);
                stream<<ui->textBrowser->toPlainText();
                file.close();

            }
        }
    });

    connect(ui->clor,&QPushButton::clicked,this,[=](){
        QColor color=QColorDialog::getColor(color,this);
        ui->textEdit->setTextColor(color);
    });


}

chat::~chat()
{
    delete ui;
}

void chat::on_pushButton_10_clicked()
{
    this->close();
}

void chat::on_pushButton_11_clicked()
{
    this->close();
}

void chat::on_pushButton_12_clicked()
{
    this->showMinimized();
}
void chat::mousePressEvent(QMouseEvent *e){
    if(e->button()==Qt::LeftButton){
        p=e->globalPos()-this->frameGeometry().topLeft();
    }
    else if(e->button()==Qt::RightButton){
        return;
    }
}
void chat::mouseMoveEvent(QMouseEvent *e){
    if(e->buttons()==Qt::LeftButton){//获取的长时间的按键是左键
        this->move(e->globalPos()-p);
    }
}



